package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class ItemCatServiceImpl implements ItemCatService{

    @Autowired
    private ItemCatMapper itemCatMapper;


    public Map<Integer,List<ItemCat>> getMap(){
        Map<Integer,List<ItemCat>> map = new HashMap<>();
        //查询所有的数据库记录
        List<ItemCat> list = itemCatMapper.selectList(null);
        //1.遍历数据
        for (ItemCat itemCat : list){
            //获取parentId
            int parentId = itemCat.getParentId();
            if (map.containsKey(parentId)){//判断集合中是否有key
                //表示数据存在，将自己追加
                map.get(parentId).add(itemCat);
            }else {
                //key不存在，定义list集合，将自己作为第一个元素追加
                List<ItemCat> childrenList = new ArrayList<>();
                childrenList.add(itemCat);
                //将数据保存到map集合中
                map.put(parentId,childrenList);
            }
        }
        return map;
    }
    //
    public List<ItemCat> getTwoList(Map<Integer,List<ItemCat>> map) {
        //1.先查询一级菜单数据
        List<ItemCat> oneList = map.get(0);
        //2.遍历每一个级菜单去封装二级数据
        for (ItemCat oneItemCat : oneList) {
            //parent_id = 一级ID
            int parentId = oneItemCat.getId();
            //查询二级数据
            List<ItemCat> towList = map.get(parentId);
            oneItemCat.setChildren(towList);
        }
        //返回一级菜单数据
        return oneList;

    }

    public List<ItemCat> getThreeList(Map<Integer ,List<ItemCat>> map){
        //1.获取1-2数据信息 包含了2级的children
        List<ItemCat> oneList = getTwoList(map);
        //2.遍历一级数据，获取二级数据
        for (ItemCat oneItemCat : oneList){
            List<ItemCat> towList = oneItemCat.getChildren();
            if (towList == null || towList.size()==0){
                //跳过本次循环，进入下一次循环
                continue;
            }
            //3.遍历二级数据，查询三级信息
            for (ItemCat towItemCat : towList){
                //查询三级 parentId = 二级ID
                int parentId = towItemCat.getId();
                List<ItemCat> threeList = map.get(parentId);
                //将三级封装到二级中
                towItemCat.setChildren(threeList);
            }
        }
        return oneList;
    }

    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        long startTime = System.currentTimeMillis();
        //获取所有集合数据
        Map<Integer,List<ItemCat>> map = getMap();
        if (level == 1){
            //1.一级商品分类信息
            return map.get(0);
        }
        if (level == 2){ //获取一级菜单和二级菜单
            return getTwoList(map);

        }
        //获取三级菜单数据 1-2-3
        List<ItemCat> allList = getThreeList(map);
        long endTime = System.currentTimeMillis();
        System.out.println("耗时:"+(endTime-startTime)+"毫秒");
        return allList;
    }

    @Transactional
    @Override
    public void saveItemCat(ItemCat itemCat) {
        //Date date = new Date();
        itemCat.setStatus(true);//.setCreated(date).setUpdated(date);
        itemCatMapper.insert(itemCat);
    }

    @Transactional
    @Override
    public void deleteItemCat(ItemCat itemCat) {
        if (itemCat.getLevel() == 3){
            int id = itemCat.getId();
            itemCatMapper.deleteById(id);
            return;
        }
        if (itemCat.getLevel() == 2){//id = 二级id
            int id = itemCat.getId();
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id",id);
            //删除的是三级数据
            itemCatMapper.delete(queryWrapper);
            //再删除自己
            itemCatMapper.deleteById(id);
            return;

        }
        //1.查询二级数据  sql: where parent_id = id
        QueryWrapper queryWrapper = new QueryWrapper();
        int id = itemCat.getId();
        queryWrapper.eq("parent_id",id);
        //由于是删除的业务，只需要获取id即可，所以使用objs
        List idList = itemCatMapper.selectObjs(queryWrapper);

        //判断是否有二级数据，如果有直接删除，如果没有直接删除一级
        if (idList.size()>0){
            //根据二级Id删除三级数据 sql where parent_id in(1,2,3)
            queryWrapper.clear();
            queryWrapper.in("parent_id",idList);
            itemCatMapper.delete(queryWrapper);
            //将所有的1-2级的ID，封装到一个集合中
            idList.add(id);
            //将所有的1级2级全部删除  sql where id in (xx,xx,xx,xx)
            itemCatMapper.deleteBatchIds(idList);
        }else {
            itemCatMapper.deleteById(id);
        }
    }

    @Transactional
    @Override
    public void updateItemCat(ItemCat itemCat) {

//        itemCatMapper.updateById(itemCat);
        ItemCat temp = new ItemCat();
        temp.setId(itemCat.getId()).setName(itemCat.getName());
        itemCatMapper.updateById(temp);
    }

    @Transactional
    @Override
    public void updateStatus(ItemCat itemCat) {

        itemCatMapper.updateById(itemCat);
    }
//@Override
//@Transactional
//public void deleteItemCat(ItemCat itemCat) {
//    if (itemCat.getLevel() == 3) {
//        int id = itemCat.getId();
//        itemCatMapper.deleteById(id);
//        return;
//    }
//    if (itemCat.getLevel() == 2) { //id=二级id
//        //Sql: xxx where parent_id = 二级ID
//        int id = itemCat.getId();
//        QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("parent_id", id);
//        //删除的是三级数据
//        itemCatMapper.delete(queryWrapper);
//        //再删除自己
//        itemCatMapper.deleteById(id);
//        return;
//    }
//
////    /**
////     * 删除一级菜单
////     */
////    //1.查询二级数据  Sql: where parent_id=id
////    QueryWrapper queryWrapper = new QueryWrapper();
////    int id = itemCat.getId();
////    queryWrapper.eq("parent_id", id);
////    //由于是删除的业务,只需要获取id即可 所以使用objs
////    List idList = itemCatMapper.selectObjs(queryWrapper);
////
////    //判断是否有二级数据.如果有直接删除.如果没有直接删除一级
////    if (idList.size() > 0) {
////        //根据二级Id删除三级数据 Sql where parent_id in (1,2,3)
////        queryWrapper.clear();
////        queryWrapper.in("parent_id", idList);
////        itemCatMapper.delete(queryWrapper);
////        //将所有的1-2级的ID,封装到一个集合中
////        idList.add(id);
////        //将所有1级2级全部删除  Sql where id in (xx,xx,xx,xx)
////        itemCatMapper.deleteBatchIds(idList);
////    } else {
////        itemCatMapper.deleteById(id);
////    }
////}

}
